package com.db.person.service;

import com.db.person.bean.Document;
import com.db.person.mapper.DocumentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@Service
public class DocumentServiceImpl implements IDocumentService{

    @Autowired
    private DocumentMapper documentMapper;

    @Override
    public List<Document> getAll() {
        return documentMapper.getAll();
    }

    public void insert(Document document, MultipartFile file) throws Exception{

        documentMapper.insert(document);

        //2.文件的上传 D:\personal\down
        String fname = file.getOriginalFilename();
        String path = "D:\\personal\\down";
        Integer id = document.getId();
        int index = fname.lastIndexOf("."); //fname = tt.dd.doc  5
        String fnameSuffix = fname.substring(index);//文件的后缀名

        File f = new File(path+File.separator+id+fnameSuffix);

        file.transferTo(f);//把文件写到指定的位置

    }
}
